package com.wdzl.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import cn.com.zcp.util.JDateChooser;

import com.wdzl.dao.SchoolDao;
import com.wdzl.dao.StudentDao;
import com.wdzl.entity.Student;
import com.wdzl.util.DateUtil;


public class SearchFrame extends JFrame {

	private JTextField textFieldEndDate;
	private JTextField textFieldStartDate;
	private JComboBox comboBoxSchool;
	private JTextField textFieldStuName;
	private JTable table;
	private Vector<String> header;

	/**
	 * Create the frame
	 */
	public SearchFrame() {
		super();
		setTitle("高级查询");
		getContentPane().setLayout(null);
		setBounds(100, 100, 581, 403);
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 553, 250);
		getContentPane().add(scrollPane);

		header = new Vector<String>();
		header.add("学员编号");
		header.add("姓名");
		header.add("性别");
		header.add("年龄");
		header.add("手机号码");
		header.add("所属学校");
		header.add("电子邮箱");
		header.add("添加日期");

		Vector<Vector<Object>> data = null;


		table = new JTable(data, header);
		scrollPane.setViewportView(table);

		final JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(Color.black, 1, false));
		panel.setLayout(null);
		panel.setBounds(10, 266, 553, 93);
		getContentPane().add(panel);

		final JLabel label = new JLabel();
		label.setBounds(30, 15, 66, 18);
		panel.add(label);
		label.setText("学员姓名");

		textFieldStuName = new JTextField();
		textFieldStuName.setBounds(95, 15, 108, 22);
		panel.add(textFieldStuName);

		final JLabel label_1 = new JLabel();
		label_1.setBounds(215, 15, 53, 18);
		panel.add(label_1);
		label_1.setText("所属学校");

		comboBoxSchool = new JComboBox();
		comboBoxSchool.setBounds(280, 15, 106, 22);
		SchoolDao dao = new SchoolDao();
		Vector<String> schoolnames = dao.querySchool();
		schoolnames.add(0, "--请选择--");

		comboBoxSchool.setModel(new DefaultComboBoxModel(schoolnames));
		panel.add(comboBoxSchool);

		final JButton button = new JButton();
		button.setBounds(425, 47, 106, 28);
		panel.add(button);
		button.addActionListener(new ActionListener() {
			//查询事件
			public void actionPerformed(final ActionEvent e) {
				String sname = textFieldStuName.getText();
				String school = comboBoxSchool.getSelectedItem().toString();
				if(comboBoxSchool.getSelectedIndex()==0){
					school = "";
				}
				String startTimeStr = textFieldStartDate.getText();
				String endTimeStr = textFieldEndDate.getText();

				//把条件封装到实体对象
				Student student = new Student();
				student.setSname(sname);
				student.setSchoolname(school);

				if(startTimeStr!=null && !startTimeStr.trim().equals("")){
					student.setStartDate(DateUtil.toSqlDate(startTimeStr));
				}
				if(endTimeStr!=null && !endTimeStr.trim().equals("")){
					student.setEndDate(DateUtil.toSqlDate(endTimeStr));
				}

				//根据条件查询并获取结果
				StudentDao dao = new StudentDao();
				Vector<Vector<Object>> data = dao.search(student);

				//把数据填充到表格
				DefaultTableModel mode = (DefaultTableModel) table.getModel();
				mode.setDataVector(data, header);
			}
		});
		button.setText("查询");

		final JLabel label_2 = new JLabel();
		label_2.setText("开始日期");
		label_2.setBounds(30, 50, 66, 18);
		panel.add(label_2);

		textFieldStartDate = new JTextField();
		textFieldStartDate.setBounds(95, 50, 106, 22);


		textFieldEndDate = new JTextField();
		textFieldEndDate.setBounds(280, 50, 106, 22);


		//使用日历组件
		JDateChooser jdc = JDateChooser.getInstance("yyyy年MM月dd日");
		jdc.register(textFieldStartDate);

		jdc = JDateChooser.getInstance("yyyy年MM月dd日");
		jdc.register(textFieldEndDate);

		panel.add(textFieldStartDate);
		panel.add(textFieldEndDate);

		final JLabel label_3 = new JLabel();
		label_3.setText("结束日期");
		label_3.setBounds(215, 50, 53, 18);
		panel.add(label_3);

		final JButton button_1 = new JButton();
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				//显示分布图
				new PieImageDialog().setVisible(true);

			}
		});
		button_1.setText("学员分布图");
		button_1.setBounds(425, 10, 106, 28);
		panel.add(button_1);
		//
	}


}
